package com.guan.serviceUcenter.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.guan.commonutils.JwtUtils;
import com.guan.commonutils.MD5;
import com.guan.serviceUcenter.entity.UcenterMember;
import com.guan.serviceUcenter.entity.vo.RegisterVo;
import com.guan.serviceUcenter.mapper.UcenterMemberMapper;
import com.guan.serviceUcenter.service.UcenterMemberService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.guan.servicebase.exceptonhandler.GuliException;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 会员表 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2023-04-18
 */
@Service
public class UcenterMemberServiceImpl extends ServiceImpl<UcenterMemberMapper, UcenterMember> implements UcenterMemberService {

    @Autowired
    private RedisTemplate<String,String> redisTemplate;

    @Override
    public String loginUser(UcenterMember member) {
        //获取手机号 密码
        String mobile = member.getMobile();
        String password = member.getPassword();

        if (StringUtils.isBlank(mobile) || StringUtils.isBlank(password)){
            throw new GuliException(20001,"登录失败");
        }

        //判断手机号对不
        QueryWrapper<UcenterMember> wrapper = new QueryWrapper<>();
        wrapper.eq("mobile",mobile);
        UcenterMember ucenterMember = baseMapper.selectOne(wrapper);
        if (ucenterMember == null){
            throw new GuliException(20001,"登录失败");
        }

        if (!MD5.encrypt(password).equals(ucenterMember.getPassword())){
            throw new GuliException(20001,"登录失败");
        }

        //判断用户是否禁用
        if (ucenterMember.getIsDisabled()){
            throw new GuliException(20001,"登录失败");
        }

        //登录成功 生成token字符窜
        String jwtToken = JwtUtils.getJwtToken(ucenterMember.getId(), ucenterMember.getNickname());

        return jwtToken;
    }

    /**
     * @date: 2023/4/19 8:38
     * @author: 管奇安
     * @description: 注册
     * @param: [registerVo]
     * @return: void
     **/
    @Override
    public void register(RegisterVo registerVo) {

        String code = registerVo.getCode(); //验证码
        String mobile = registerVo.getMobile(); //手机号
        String nickname = registerVo.getNickname(); //昵称
        String password = registerVo.getPassword(); //密码

        if (StringUtils.isBlank(mobile) || StringUtils.isBlank(password) ||
                StringUtils.isBlank(code) || StringUtils.isBlank(nickname)){
            throw new GuliException(20001,"登录失败");
        }

        //判断验证码是否正确
        String redisCode = redisTemplate.opsForValue().get(mobile);
        if (!code.equals(redisCode)){
            throw new GuliException(20001,"登录失败");
        }

        //手机号是否重复
        QueryWrapper<UcenterMember> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("mobile",mobile);
        Integer count = baseMapper.selectCount(queryWrapper);
        if (count>0){
            throw new GuliException(20001,"该手机号已注册");
        }

        UcenterMember ucenterMember = new UcenterMember();
        ucenterMember.setMobile(mobile);
        ucenterMember.setNickname(nickname);
        ucenterMember.setPassword(MD5.encrypt(password));
        ucenterMember.setIsDisabled(false);
        ucenterMember.setIsDeleted(false);

        baseMapper.insert(ucenterMember);

    }

    @Override
    public UcenterMember getByOpenid(String openid) {
        QueryWrapper<UcenterMember> wrapper = new QueryWrapper<>();
        wrapper.eq("openid",openid);
        UcenterMember ucenterMember = baseMapper.selectOne(wrapper);
        return ucenterMember;
    }

    @Override
    public Integer countRegisterByDay(String day) {
        return baseMapper.countRegisterByDay(day);
    }
}
